home *** CD-ROM | disk | FTP | other *** search
/ AGA Toolkit '97 / The AGA Toolkit '97.iso / programming / c / make-3.75 / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-07  |  53.9 KB  |  2,055 lines

  1. /* Argument parsing and main program of GNU Make.
  2. Copyright (C) 1988, 89, 90, 91, 94, 1995 Free Software Foundation, Inc.
  3. This file is part of GNU Make.
  4.  
  5. GNU Make is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. GNU Make is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Make; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "make.h"
  20. #include "commands.h"
  21. #include "dep.h"
  22. #include "file.h"
  23. #include "variable.h"
  24. #include "job.h"
  25. #include "getopt.h"
  26. #include <assert.h>
  27. #ifdef _AMIGA
  28. #   include <dos/dos.h>
  29. #   include <proto/dos.h>
  30. #endif
  31.  
  32. #ifndef _AMIGA
  33. int __stack = 20000; /* Make sure we have 20K of stack space */
  34. #endif
  35.  
  36. extern void print_variable_data_base ();
  37. extern void print_dir_data_base ();
  38. extern void print_rule_data_base ();
  39. extern void print_file_data_base ();
  40. extern void print_vpath_data_base ();
  41.  
  42. #ifndef HAVE_UNISTD_H
  43. extern int chdir ();
  44. #endif
  45. #ifndef STDC_HEADERS
  46. #ifndef sun            /* Sun has an incorrect decl in a header.  */
  47. extern void exit ();
  48. #endif
  49. extern double atof ();
  50. #endif
  51. extern char *mktemp ();
  52.  
  53. static void log_working_directory ();
  54. static void print_data_base (), print_version ();
  55. static void decode_switches (), decode_env_switches ();
  56. static void define_makeflags ();
  57. static char *quote_as_word ();
  58.  
  59. /* The structure that describes an accepted command switch.  */
  60.  
  61. struct command_switch
  62.   {
  63.     char c;            /* The switch character.  */
  64.  
  65.     enum            /* Type of the value.  */
  66.       {
  67.     flag,            /* Turn int flag on.  */
  68.     flag_off,        /* Turn int flag off.  */
  69.     string,         /* One string per switch.  */
  70.     positive_int,        /* A positive integer.    */
  71.     floating,        /* A floating-point number (double).  */
  72.     ignore            /* Ignored.  */
  73.       } type;
  74.  
  75.     char *value_ptr;    /* Pointer to the value-holding variable.  */
  76.  
  77.     unsigned int env:1;     /* Can come from MAKEFLAGS.  */
  78.     unsigned int toenv:1;    /* Should be put in MAKEFLAGS.    */
  79.     unsigned int no_makefile:1; /* Don't propagate when remaking makefiles.  */
  80.  
  81.     char *noarg_value;    /* Pointer to value used if no argument is given.  */
  82.     char *default_value;/* Pointer to default value.  */
  83.  
  84.     char *long_name;        /* Long option name.  */
  85.     char *argdesc;        /* Descriptive word for argument.  */
  86.     char *description;        /* Description for usage message.  */
  87.   };
  88.  
  89.  
  90. /* The structure used to hold the list of strings given
  91.    in command switches of a type that takes string arguments.  */
  92.  
  93. struct stringlist
  94.   {
  95.     char **list;    /* Nil-terminated list of strings.  */
  96.     unsigned int idx;    /* Index into above.  */
  97.     unsigned int max;    /* Number of pointers allocated.  */
  98.   };
  99.  
  100.  
  101. /* The recognized command switches.  */
  102.  
  103. /* Nonzero means do not print commands to be executed (-s).  */
  104.  
  105. int silent_flag;
  106.  
  107. /* Nonzero means just touch the files
  108.    that would appear to need remaking (-t)  */
  109.  
  110. int touch_flag;
  111.  
  112. /* Nonzero means just print what commands would need to be executed,
  113.    don't actually execute them (-n).  */
  114.  
  115. int just_print_flag;
  116.  
  117. /* Print debugging trace info (-d).  */
  118.  
  119. int debug_flag = 0;
  120.  
  121. /* Environment variables override makefile definitions.  */
  122.  
  123. int env_overrides = 0;
  124.  
  125. /* Nonzero means ignore status codes returned by commands
  126.    executed to remake files.  Just treat them all as successful (-i).  */
  127.  
  128. int ignore_errors_flag = 0;
  129.  
  130. /* Nonzero means don't remake anything, just print the data base
  131.    that results from reading the makefile (-p).  */
  132.  
  133. int print_data_base_flag = 0;
  134.  
  135. /* Nonzero means don't remake anything; just return a nonzero status
  136.    if the specified targets are not up to date (-q).  */
  137.  
  138. int question_flag = 0;
  139.  
  140. /* Nonzero means do not use any of the builtin rules (-r).  */
  141.  
  142. int no_builtin_rules_flag = 0;
  143.  
  144. /* Nonzero means keep going even if remaking some file fails (-k).  */
  145.  
  146. int keep_going_flag;
  147. int default_keep_going_flag = 0;
  148.  
  149. /* Nonzero means print directory before starting and when done (-w).  */
  150.  
  151. int print_directory_flag = 0;
  152.  
  153. /* Nonzero means ignore print_directory_flag and never print the directory.
  154.    This is necessary because print_directory_flag is set implicitly.  */
  155.  
  156. int inhibit_print_directory_flag = 0;
  157.  
  158. /* Nonzero means print version information.  */
  159.  
  160. int print_version_flag = 0;
  161.  
  162. /* List of makefiles given with -f switches.  */
  163.  
  164. static struct stringlist *makefiles = 0;
  165.  
  166.  
  167. /* Number of job slots (commands that can be run at once).  */
  168.  
  169. unsigned int job_slots = 1;
  170. unsigned int default_job_slots = 1;
  171.  
  172. /* Value of job_slots that means no limit.  */
  173.  
  174. static unsigned int inf_jobs = 0;
  175.  
  176. /* Maximum load average at which multiple jobs will be run.
  177.    Negative values mean unlimited, while zero means limit to
  178.    zero load (which could be useful to start infinite jobs remotely
  179.    but one at a time locally).    */
  180. #ifndef NO_FLOAT
  181. double max_load_average = -1.0;
  182. double default_load_average = -1.0;
  183. #else
  184. int max_load_average = -1;
  185. int default_load_average = -1;
  186. #endif
  187.  
  188. /* List of directories given with -C switches.    */
  189.  
  190. static struct stringlist *directories = 0;
  191.  
  192. /* List of include directories given with -I switches.    */
  193.  
  194. static struct stringlist *include_directories = 0;
  195.  
  196. /* List of files given with -o switches.  */
  197.  
  198. static struct stringlist *old_files = 0;
  199.  
  200. /* List of files given with -W switches.  */
  201.  
  202. static struct stringlist *new_files = 0;
  203.  
  204. /* If nonzero, we should just print usage and exit.  */
  205.  
  206. static int print_usage_flag = 0;
  207.  
  208. /* If nonzero, we should print a warning message
  209.    for each reference to an undefined variable.  */
  210.  
  211. int warn_undefined_variables_flag;
  212.  
  213. /* The table of command switches.  */
  214.  
  215. static const struct command_switch switches[] =
  216.   {
  217.     { 'b', ignore, 0, 0, 0, 0, 0, 0,
  218.     0, 0,
  219.     "Ignored for compatibility" },
  220.     { 'C', string, (char *) &directories, 0, 0, 0, 0, 0,
  221.     "directory", "DIRECTORY",
  222.     "Change to DIRECTORY before doing anything" },
  223.     { 'd', flag, (char *) &debug_flag, 1, 1, 0, 0, 0,
  224.     "debug", 0,
  225.     "Print lots of debugging information" },
  226.     { 'e', flag, (char *) &env_overrides, 1, 1, 0, 0, 0,
  227.     "environment-overrides", 0,
  228.     "Environment variables override makefiles" },
  229.     { 'f', string, (char *) &makefiles, 0, 0, 0, 0, 0,
  230.     "file", "FILE",
  231.     "Read FILE as a makefile" },
  232.     { 'h', flag, (char *) &print_usage_flag, 0, 0, 0, 0, 0,
  233.     "help", 0,
  234.     "Print this message and exit" },
  235.     { 'i', flag, (char *) &ignore_errors_flag, 1, 1, 0, 0, 0,
  236.     "ignore-errors", 0,
  237.     "Ignore errors from commands" },
  238.     { 'I', string, (char *) &include_directories, 1, 1, 0, 0, 0,
  239.     "include-dir", "DIRECTORY",
  240.     "Search DIRECTORY for included makefiles" },
  241.     { 'j', positive_int, (char *) &job_slots, 1, 1, 0,
  242.     (char *) &inf_jobs, (char *) &default_job_slots,
  243.     "jobs", "N",
  244.     "Allow N jobs at once; infinite jobs with no arg" },
  245.     { 'k', flag, (char *) &keep_going_flag, 1, 1, 0,
  246.     0, (char *) &default_keep_going_flag,
  247.     "keep-going", 0,
  248.     "Keep going when some targets can't be made" },
  249. #ifndef NO_FLOAT
  250.     { 'l', floating, (char *) &max_load_average, 1, 1, 0,
  251.     (char *) &default_load_average, (char *) &default_load_average,
  252.     "load-average", "N",
  253.     "Don't start multiple jobs unless load is below N" },
  254. #else
  255.     { 'l', positive_int, (char *) &max_load_average, 1, 1, 0,
  256.     (char *) &default_load_average, (char *) &default_load_average,
  257.     "load-average", "N",
  258.     "Don't start multiple jobs unless load is below N" },
  259. #endif
  260.     { 'm', ignore, 0, 0, 0, 0, 0, 0,
  261.     0, 0,
  262.     "-b" },
  263.     { 'n', flag, (char *) &just_print_flag, 1, 1, 1, 0, 0,
  264.     "just-print", 0,
  265.     "Don't actually run any commands; just print them" },
  266.     { 'o', string, (char *) &old_files, 0, 0, 0, 0, 0,
  267.     "old-file", "FILE",
  268.     "Consider FILE to be very old and don't remake it" },
  269.     { 'p', flag, (char *) &print_data_base_flag, 1, 1, 0, 0, 0,
  270.     "print-data-base", 0,
  271.     "Print make's internal database" },
  272.     { 'q', flag, (char *) &question_flag, 1, 1, 1, 0, 0,
  273.     "question", 0,
  274.     "Run no commands; exit status says if up to date" },
  275.     { 'r', flag, (char *) &no_builtin_rules_flag, 1, 1, 0, 0, 0,
  276.     "no-builtin-rules", 0,
  277.     "Disable the built-in implicit rules" },
  278.     { 's', flag, (char *) &silent_flag, 1, 1, 0, 0, 0,
  279.     "silent", 0,
  280.     "Don't echo commands" },
  281.     { 'S', flag_off, (char *) &keep_going_flag, 1, 1, 0,
  282.     0, (char *) &default_keep_going_flag,
  283.     "no-keep-going", 0,
  284.     "Turns off -k" },
  285.     { 't', flag, (char *) &touch_flag, 1, 1, 1, 0, 0,
  286.     "touch", 0,
  287.     "Touch targets instead of remaking them" },
  288.     { 'v', flag, (char *) &print_version_flag, 1, 1, 0, 0, 0,
  289.     "version", 0,
  290.     "Print the version number of make and exit" },
  291.     { 'w', flag, (char *) &print_directory_flag, 1, 1, 0, 0, 0,
  292.     "print-directory", 0,
  293.     "Print the current directory" },
  294.     { 2, flag, (char *) &inhibit_print_directory_flag, 1, 1, 0, 0, 0,
  295.     "no-print-directory", 0,
  296.     "Turn off -w, even if it was turned on implicitly" },
  297.     { 'W', string, (char *) &new_files, 0, 0, 0, 0, 0,
  298.     "what-if", "FILE",
  299.     "Consider FILE to be infinitely new" },
  300.     { 3, flag, (char *) &warn_undefined_variables_flag, 1, 1, 0, 0, 0,
  301.     "warn-undefined-variables", 0,
  302.     "Warn when an undefined variable is referenced" },
  303.     { '\0', }
  304.   };
  305.  
  306. /* Secondary long names for options.  */
  307.  
  308. static struct option long_option_aliases[] =
  309.   {
  310.     { "quiet",          no_argument,            0, 's' },
  311.     { "stop",           no_argument,            0, 'S' },
  312.     { "new-file",       required_argument,      0, 'W' },
  313.     { "assume-new",     required_argument,      0, 'W' },
  314.     { "assume-old",     required_argument,      0, 'o' },
  315.     { "max-load",       optional_argument,      0, 'l' },
  316.     { "dry-run",        no_argument,            0, 'n' },
  317.     { "recon",          no_argument,            0, 'n' },
  318.     { "makefile",       required_argument,      0, 'f' },
  319.   };
  320.  
  321. /* The usage message prints the descriptions of options starting in
  322.    this column.  Make sure it leaves enough room for the longest
  323.    description to fit in less than 80 characters.  */
  324.  
  325. #define DESCRIPTION_COLUMN    30
  326.  
  327. /* List of goal targets.  */
  328.  
  329. static struct dep *goals, *lastgoal;
  330.  
  331. /* List of variables which were defined on the command line
  332.    (or, equivalently, in MAKEFLAGS).  */
  333.  
  334. struct command_variable
  335.   {
  336.     struct command_variable *next;
  337.     struct variable *variable;
  338.   };
  339. static struct command_variable *command_variables;
  340.  
  341. /* The name we were invoked with.  */
  342.  
  343. char *program;
  344.  
  345. /* Our current directory before processing any -C options.  */
  346.  
  347. char *directory_before_chdir;
  348.  
  349. /* Our current directory after processing all -C options.  */
  350.  
  351. char *starting_directory;
  352.  
  353. /* Value of the MAKELEVEL variable at startup (or 0).  */
  354.  
  355. unsigned int makelevel;
  356.  
  357. /* First file defined in the makefile whose name does not
  358.    start with `.'.  This is the default to remake if the
  359.    command line does not specify.  */
  360.  
  361. struct file *default_goal_file;
  362.  
  363. /* Pointer to structure for the file .DEFAULT
  364.    whose commands are used for any file that has none of its own.
  365.    This is zero if the makefiles do not define .DEFAULT.  */
  366.  
  367. struct file *default_file;
  368.  
  369. /* Nonzero if we have seen the magic `.POSIX' target.
  370.    This turns on pedantic compliance with POSIX.2.  */
  371.  
  372. int posix_pedantic;
  373.  
  374. /* Mask of signals that are being caught with fatal_error_signal.  */
  375.  
  376. #ifdef    POSIX
  377. sigset_t fatal_signal_set;
  378. #else
  379. #ifdef    HAVE_SIGSETMASK
  380. int fatal_signal_mask;
  381. #endif
  382. #endif
  383.  
  384. static struct file *
  385. enter_command_line_file (name)
  386.      char *name;
  387. {
  388.   if (name[0] == '~')
  389.     {
  390.       char *expanded = tilde_expand (name);
  391.       if (expanded != 0)
  392.     name = expanded;    /* Memory leak; I don't care.  */
  393.     }
  394.  
  395.   /* This is also done in parse_file_seq, so this is redundant
  396.      for names read from makefiles.  It is here for names passed
  397.      on the command line.  */
  398.   while (name[0] == '.' && name[1] == '/' && name[2] != '\0')
  399.     {
  400.       name += 2;
  401.       while (*name == '/')
  402.     /* Skip following slashes: ".//foo" is "foo", not "/foo".  */
  403.     ++name;
  404.     }
  405.  
  406.   if (*name == '\0')
  407.     {
  408.       /* It was all slashes!  Move back to the dot and truncate
  409.      it after the first slash, so it becomes just "./".  */
  410.       do
  411.     --name;
  412.       while (name[0] != '.');
  413.       name[2] = '\0';
  414.     }
  415.  
  416.   return enter_file (savestring (name, strlen (name)));
  417. }
  418.  
  419. /* Toggle -d on receipt of SIGUSR1.  */
  420.  
  421. static RETSIGTYPE
  422. debug_signal_handler (sig)
  423.      int sig;
  424. {
  425.   debug_flag = ! debug_flag;
  426. }
  427.  
  428. #ifndef _AMIGA
  429. int
  430. main (argc, argv, envp)
  431.      int argc;
  432.      char **argv;
  433.      char **envp;
  434. #else
  435. int main (int argc, char ** argv)
  436. #endif
  437. {
  438.   extern void init_dir ();
  439.   extern RETSIGTYPE fatal_error_signal (), child_handler ();
  440.   register struct file *f;
  441.   register unsigned int i;
  442.   char **p;
  443.   struct dep *read_makefiles;
  444.   PATH_VAR (current_directory);
  445.  
  446.   default_goal_file = 0;
  447.   reading_filename = 0;
  448.   reading_lineno_ptr = 0;
  449.  
  450. #if !defined (HAVE_STRSIGNAL) && !defined (HAVE_SYS_SIGLIST)
  451.   signame_init ();
  452. #endif
  453.  
  454. #ifdef    POSIX
  455.   sigemptyset (&fatal_signal_set);
  456. #define ADD_SIG(sig)    sigaddset (&fatal_signal_set, sig)
  457. #else
  458. #ifdef    HAVE_SIGSETMASK
  459.   fatal_signal_mask = 0;
  460. #define ADD_SIG(sig)    fatal_signal_mask |= sigmask (sig)
  461. #else
  462. #define ADD_SIG(sig)
  463. #endif
  464. #endif
  465.  
  466. #define FATAL_SIG(sig)                                                        \
  467.   if (signal ((sig), fatal_error_signal) == SIG_IGN)                          \
  468.     (void) signal ((sig), SIG_IGN);                                           \
  469.   else                                          \
  470.     ADD_SIG (sig);
  471.  
  472. #ifdef SIGHUP
  473.   FATAL_SIG (SIGHUP);
  474. #endif
  475. #ifdef SIGQUIT
  476.   FATAL_SIG (SIGQUIT);
  477. #endif
  478.   FATAL_SIG (SIGINT);
  479.   FATAL_SIG (SIGTERM);
  480.  
  481. #ifdef    SIGDANGER
  482.   FATAL_SIG (SIGDANGER);
  483. #endif
  484. #ifdef SIGXCPU
  485.   FATAL_SIG (SIGXCPU);
  486. #endif
  487. #ifdef SIGXFSZ
  488.   FATAL_SIG (SIGXFSZ);
  489. #endif
  490.  
  491. #undef    FATAL_SIG
  492.  
  493.   /* Make sure stdout is line-buffered.  */
  494.  
  495. #ifdef    HAVE_SETLINEBUF
  496.   setlinebuf (stdout);
  497. #else
  498. #ifndef SETVBUF_REVERSED
  499.   setvbuf (stdout, (char *) 0, _IOLBF, BUFSIZ);
  500. #else    /* setvbuf not reversed.  */
  501.   /* Some buggy systems lose if we pass 0 instead of allocating ourselves.  */
  502.   setvbuf (stdout, _IOLBF, xmalloc (BUFSIZ), BUFSIZ);
  503. #endif    /* setvbuf reversed.  */
  504. #endif    /* setlinebuf missing.    */
  505.  
  506.   /* Initialize the directory hashing code.  */
  507.   init_dir ();
  508.  
  509.   /* Figure out where this program lives.  */
  510.  
  511.   if (argv[0] == 0)
  512.     argv[0] = "";
  513.   if (argv[0][0] == '\0')
  514.     program = "make";
  515.   else
  516.     {
  517.       program = rindex (argv[0], '/');
  518. #ifdef __MSDOS__
  519.       if (program == 0)
  520.     program = rindex (argv[0], '\\');
  521.       if (program == 0)
  522.     program = rindex (argv[0], ':');
  523. #endif
  524.       if (program == 0)
  525.     program = argv[0];
  526.       else
  527.     ++program;
  528.     }
  529.  
  530.   /* Set up to access user data (files).  */
  531.   user_access ();
  532.  
  533.   /* Figure out where we are.  */
  534.  
  535.   if (getcwd (current_directory, GET_PATH_MAX) == 0)
  536.     {
  537. #ifdef    HAVE_GETCWD
  538.       perror_with_name ("getcwd: ", "");
  539. #else
  540.       error ("getwd: %s", current_directory);
  541. #endif
  542.       current_directory[0] = '\0';
  543.       directory_before_chdir = 0;
  544.     }
  545.   else
  546.     directory_before_chdir = savestring (current_directory,
  547.                      strlen (current_directory));
  548.  
  549.   /* Read in variables from the environment.  It is important that this be
  550.      done before $(MAKE) is are figured out so its definitions will not be
  551.      one from the environment.    */
  552.  
  553. #ifndef _AMIGA
  554.   for (i = 0; envp[i] != 0; ++i)
  555.     {
  556.       register char *ep = envp[i];
  557.       while (*ep != '=')
  558.     ++ep;
  559.       /* The result of pointer arithmetic is cast to unsigned int for
  560.      machines where ptrdiff_t is a different size that doesn't widen
  561.      the same.  */
  562.       define_variable (envp[i], (unsigned int) (ep - envp[i]),
  563.                ep + 1, o_env, 1)
  564.     /* Force exportation of every variable culled from the environment.
  565.        We used to rely on target_environment's v_default code to do this.
  566.        But that does not work for the case where an environment variable
  567.        is redefined in a makefile with `override'; it should then still
  568.        be exported, because it was originally in the environment.  */
  569.     ->export = v_export;
  570.     }
  571. #else /* For Amiga, read the ENV: device, ignoring all dirs */
  572.     {
  573.     BPTR env, file, old;
  574.     char buffer[1024];
  575.     int len;
  576.     __aligned struct FileInfoBlock fib;
  577.  
  578.     env = Lock ("ENV:", ACCESS_READ);
  579.     if (env)
  580.     {
  581.         old = CurrentDir (DupLock(env));
  582.         Examine (env, &fib);
  583.  
  584.         while (ExNext (env, &fib))
  585.         {
  586.         if (fib.fib_DirEntryType < 0) /* File */
  587.         {
  588.             file = Open (fib.fib_FileName, MODE_OLDFILE);
  589.  
  590.             if (file)
  591.             {
  592.             len = Read (file, buffer, sizeof (buffer)-1);
  593.             buffer[len] = 0;
  594.  
  595.             define_variable (fib.fib_FileName,
  596.                 strlen (fib.fib_FileName),
  597.                 buffer, o_env, 1)->export = v_export;
  598.             }
  599.         }
  600.         }
  601.         UnLock (env);
  602.         UnLock(CurrentDir(old));
  603.     }
  604.     }
  605. #endif
  606.  
  607.   /* Decode the switches.  */
  608.  
  609.   decode_env_switches ("MAKEFLAGS", 9);
  610. #if 0
  611.   /* People write things like:
  612.     MFLAGS="CC=gcc -pipe" "CFLAGS=-g"
  613.      and we set the -p, -i and -e switches.  Doesn't seem quite right.  */
  614.   decode_env_switches ("MFLAGS", 6);
  615. #endif
  616.   decode_switches (argc, argv, 0);
  617.  
  618.   /* Print version information.  */
  619.  
  620.   if (print_version_flag || print_data_base_flag || debug_flag)
  621.     print_version ();
  622.  
  623.   /* `make --version' is supposed to just print the version and exit.  */
  624.   if (print_version_flag)
  625.     die (0);
  626.  
  627. #ifndef __MSDOS__
  628.   /* Set the "MAKE_COMMAND" variable to the name we were invoked with.
  629.      (If it is a relative pathname with a slash, prepend our directory name
  630.      so the result will run the same program regardless of the current dir.
  631.      If it is a name with no slash, we can only hope that PATH did not
  632.      find it in the current directory.)  */
  633.  
  634.   if (current_directory[0] != '\0'
  635.       && argv[0] != 0 && argv[0][0] != '/' && index (argv[0], '/') != 0)
  636.     argv[0] = concat (current_directory, "/", argv[0]);
  637. #endif
  638.  
  639.   /* The extra indirection through $(MAKE_COMMAND) is done
  640.      for hysterical raisins.  */
  641.   (void) define_variable ("MAKE_COMMAND", 12, argv[0], o_default, 0);
  642.   (void) define_variable ("MAKE", 4, "$(MAKE_COMMAND)", o_default, 1);
  643.  
  644.   if (command_variables != 0)
  645.     {
  646.       struct command_variable *cv;
  647.       struct variable *v;
  648.       unsigned int len = 0;
  649.       char *value, *p;
  650.  
  651.       /* Figure out how much space will be taken up by the command-line
  652.      variable definitions.    */
  653.       for (cv = command_variables; cv != 0; cv = cv->next)
  654.     {
  655.       v = cv->variable;
  656.       len += 2 * strlen (v->name);
  657.       if (! v->recursive)
  658.         ++len;
  659.       ++len;
  660.       len += 2 * strlen (v->value);
  661.     }
  662.  
  663.       /* Now allocate a buffer big enough and fill it.    */
  664.       p = value = (char *) alloca (len);
  665.       for (cv = command_variables; cv != 0; cv = cv->next)
  666.     {
  667.       v = cv->variable;
  668.       p = quote_as_word (p, v->name, 0);
  669.       if (! v->recursive)
  670.         *p++ = ':';
  671.       *p++ = '=';
  672.       p = quote_as_word (p, v->value, 0);
  673.       *p++ = ' ';
  674.     }
  675.       p[-1] = '\0';             /* Kill the final space and terminate.  */
  676.  
  677.       /* Define an unchangeable variable with a name that no POSIX.2
  678.      makefile could validly use for its own variable.  */
  679.       (void) define_variable ("-*-command-variables-*-", 23,
  680.                   value, o_automatic, 0);
  681.  
  682.       /* Define the variable; this will not override any user definition.
  683.      Normally a reference to this variable is written into the value of
  684.      MAKEFLAGS, allowing the user to override this value to affect the
  685.      exported value of MAKEFLAGS.  In POSIX-pedantic mode, we cannot
  686.      allow the user's setting of MAKEOVERRIDES to affect MAKEFLAGS, so
  687.      a reference to this hidden variable is written instead. */
  688.       (void) define_variable ("MAKEOVERRIDES", 13,
  689.                   "${-*-command-variables-*-}", o_env, 1);
  690.     }
  691.  
  692.   /* If there were -C flags, move ourselves about.  */
  693.   if (directories != 0)
  694.     for (i = 0; directories->list[i] != 0; ++i)
  695.       {
  696.     char *dir = directories->list[i];
  697.     if (dir[0] == '~')
  698.       {
  699.         char *expanded = tilde_expand (dir);
  700.         if (expanded != 0)
  701.           dir = expanded;
  702.       }
  703.     if (chdir (dir) < 0)
  704.       pfatal_with_name (dir);
  705.     if (dir != directories->list[i])
  706.       free (dir);
  707.       }
  708.  
  709.   /* Figure out the level of recursion.  */
  710.   {
  711.     struct variable *v = lookup_variable ("MAKELEVEL", 9);
  712.     if (v != 0 && *v->value != '\0' && *v->value != '-')
  713.       makelevel = (unsigned int) atoi (v->value);
  714.     else
  715.       makelevel = 0;
  716.   }
  717.  
  718.   /* Except under -s, always do -w in sub-makes and under -C.  */
  719.   if (!silent_flag && (directories != 0 || makelevel > 0))
  720.     print_directory_flag = 1;
  721.  
  722.   /* Let the user disable that with --no-print-directory.  */
  723.   if (inhibit_print_directory_flag)
  724.     print_directory_flag = 0;
  725.  
  726.   /* Construct the list of include directories to search.  */
  727.  
  728.   construct_include_path (include_directories == 0 ? (char **) 0
  729.               : include_directories->list);
  730.  
  731.   /* Figure out where we are now, after chdir'ing.  */
  732.   if (directories == 0)
  733.     /* We didn't move, so we're still in the same place.  */
  734.     starting_directory = current_directory;
  735.   else
  736.     {
  737.       if (getcwd (current_directory, GET_PATH_MAX) == 0)
  738.     {
  739. #ifdef    HAVE_GETCWD
  740.       perror_with_name ("getcwd: ", "");
  741. #else
  742.       error ("getwd: %s", current_directory);
  743. #endif
  744.       starting_directory = 0;
  745.     }
  746.       else
  747.     starting_directory = current_directory;
  748.     }
  749.  
  750.   /* Tell the user where he is.  */
  751.  
  752.   if (print_directory_flag)
  753.     log_working_directory (1);
  754.  
  755.   /* Read any stdin makefiles into temporary files.  */
  756.  
  757.   if (makefiles != 0)
  758.     {
  759.       register unsigned int i;
  760.       for (i = 0; i < makefiles->idx; ++i)
  761.     if (makefiles->list[i][0] == '-' && makefiles->list[i][1] == '\0')
  762.       {
  763.         /* This makefile is standard input.  Since we may re-exec
  764.            and thus re-read the makefiles, we read standard input
  765.            into a temporary file and read from that.  */
  766.         FILE *outfile;
  767.  
  768.         /* Make a unique filename.    */
  769. #ifdef HAVE_MKTEMP
  770.         static char name[] = "/tmp/GmXXXXXX";
  771.         (void) mktemp (name);
  772. #else
  773.         static char name[L_tmpnam];
  774.         (void) tmpnam (name);
  775. #endif
  776.  
  777.         outfile = fopen (name, "w");
  778.         if (outfile == 0)
  779.           pfatal_with_name ("fopen (temporary file)");
  780.         while (!feof (stdin))
  781.           {
  782.         char buf[2048];
  783.         int n = fread (buf, 1, sizeof(buf), stdin);
  784.         if (n > 0 && fwrite (buf, 1, n, outfile) != n)
  785.           pfatal_with_name ("fwrite (temporary file)");
  786.           }
  787.         /* Try to make sure we won't remake the temporary
  788.            file when we are re-exec'd.  Kludge-o-matic!  */
  789.         fprintf (outfile, "%s:;\n", name);
  790.         (void) fclose (outfile);
  791.  
  792.         /* Replace the name that read_all_makefiles will
  793.            see with the name of the temporary file.  */
  794.         {
  795.           char *temp;
  796.           /* SGI compiler requires alloca's result be assigned simply.  */
  797.           temp = (char *) alloca (sizeof (name));
  798.           bcopy (name, temp, sizeof (name));
  799.           makefiles->list[i] = temp;
  800.         }
  801.  
  802.         /* Make sure the temporary file will not be remade.  */
  803.         f = enter_file (savestring (name, sizeof name - 1));
  804.         f->updated = 1;
  805.         f->update_status = 0;
  806.         f->command_state = cs_finished;
  807.         /* Let it be removed when we're done.  */
  808.         f->intermediate = 1;
  809.         /* But don't mention it.  */
  810.         f->dontcare = 1;
  811.       }
  812.     }
  813.  
  814.   /* Set up to handle children dying.  This must be done before
  815.      reading in the makefiles so that `shell' function calls will work.  */
  816.  
  817. #ifdef SIGCHLD
  818.   (void) signal (SIGCHLD, child_handler);
  819. #endif
  820. #ifdef SIGCLD
  821.   (void) signal (SIGCLD, child_handler);
  822. #endif
  823.  
  824.   /* Let the user send us SIGUSR1 to toggle the -d flag during the run.  */
  825. #ifdef SIGUSR1
  826.   (void) signal (SIGUSR1, debug_signal_handler);
  827. #endif
  828.  
  829.   /* Define the initial list of suffixes for old-style rules.  */
  830.  
  831.   set_default_suffixes ();
  832.  
  833.   /* Define the file rules for the built-in suffix rules.  These will later
  834.      be converted into pattern rules.  We used to do this in
  835.      install_default_implicit_rules, but since that happens after reading
  836.      makefiles, it results in the built-in pattern rules taking precedence
  837.      over makefile-specified suffix rules, which is wrong.  */
  838.  
  839.   install_default_suffix_rules ();
  840.  
  841.   /* Define some internal and special variables.  */
  842.  
  843.   define_automatic_variables ();
  844.  
  845.   /* Set up the MAKEFLAGS and MFLAGS variables
  846.      so makefiles can look at them.  */
  847.  
  848.   define_makeflags (0, 0);
  849.  
  850.   /* Define the default variables.  */
  851.   define_default_variables ();
  852.  
  853.   /* Read all the makefiles.  */
  854.  
  855.   default_file = enter_file (".DEFAULT");
  856.  
  857.   read_makefiles
  858.     = read_all_makefiles (makefiles == 0 ? (char **) 0 : makefiles->list);
  859.  
  860.   /* Decode switches again, in case the variables were set by the makefile.  */
  861.   decode_env_switches ("MAKEFLAGS", 9);
  862. #if 0
  863.   decode_env_switches ("MFLAGS", 6);
  864. #endif
  865.  
  866.   /* Set up MAKEFLAGS and MFLAGS again, so they will be right.    */
  867.  
  868.   define_makeflags (1, 0);
  869.  
  870.   /* Make each `struct dep' point at the `struct file' for the file
  871.      depended on.  Also do magic for special targets.  */
  872.  
  873.   snap_deps ();
  874.  
  875.   /* Convert old-style suffix rules to pattern rules.  It is important to
  876.      do this before installing the built-in pattern rules below, so that
  877.      makefile-specified suffix rules take precedence over built-in pattern
  878.      rules.  */
  879.  
  880.   convert_to_pattern ();
  881.  
  882.   /* Install the default implicit pattern rules.
  883.      This used to be done before reading the makefiles.
  884.      But in that case, built-in pattern rules were in the chain
  885.      before user-defined ones, so they matched first.  */
  886.  
  887.   install_default_implicit_rules ();
  888.  
  889.   /* Compute implicit rule limits.  */
  890.  
  891.   count_implicit_rule_limits ();
  892.  
  893.   /* Construct the listings of directories in VPATH lists.  */
  894.  
  895.   build_vpath_lists ();
  896.  
  897.   /* Mark files given with -o flags as very old (00:00:01.00 Jan 1, 1970)
  898.      and as having been updated already, and files given with -W flags as
  899.      brand new (time-stamp as far as possible into the future).  */
  900.  
  901.   if (old_files != 0)
  902.     for (p = old_files->list; *p != 0; ++p)
  903.       {
  904.     f = enter_command_line_file (*p);
  905.     f->last_mtime = (time_t) 1;
  906.     f->updated = 1;
  907.     f->update_status = 0;
  908.     f->command_state = cs_finished;
  909.       }
  910.  
  911.   if (new_files != 0)
  912.     {
  913.       for (p = new_files->list; *p != 0; ++p)
  914.     {
  915.       f = enter_command_line_file (*p);
  916.       f->last_mtime = NEW_MTIME;
  917.     }
  918.     }
  919.  
  920.   if (read_makefiles != 0)
  921.     {
  922.       /* Update any makefiles if necessary.  */
  923.  
  924.       time_t *makefile_mtimes = 0;
  925.       unsigned int mm_idx = 0;
  926.  
  927.       if (debug_flag)
  928.     puts ("Updating makefiles....");
  929.  
  930.       /* Remove any makefiles we don't want to try to update.
  931.      Also record the current modtimes so we can compare them later.  */
  932.       {
  933.     register struct dep *d, *last;
  934.     last = 0;
  935.     d = read_makefiles;
  936.     while (d != 0)
  937.       {
  938.         register struct file *f = d->file;
  939.         if (f->double_colon)
  940.           for (f = f->double_colon; f != NULL; f = f->prev)
  941.         {
  942.           if (f->deps == 0 && f->cmds != 0)
  943.             {
  944.               /* This makefile is a :: target with commands, but
  945.              no dependencies.  So, it will always be remade.
  946.              This might well cause an infinite loop, so don't
  947.              try to remake it.  (This will only happen if
  948.              your makefiles are written exceptionally
  949.              stupidly; but if you work for Athena, that's how
  950.              you write your makefiles.)  */
  951.  
  952.               if (debug_flag)
  953.             printf ("Makefile `%s' might loop; not remaking it.\n",
  954.                 f->name);
  955.  
  956.               if (last == 0)
  957.             read_makefiles = d->next;
  958.               else
  959.             last->next = d->next;
  960.  
  961.               /* Free the storage.  */
  962.               free ((char *) d);
  963.  
  964.               d = last == 0 ? 0 : last->next;
  965.  
  966.               break;
  967.             }
  968.         }
  969.         if (f == NULL || !f->double_colon)
  970.           {
  971.         if (makefile_mtimes == 0)
  972.           makefile_mtimes = (time_t *) xmalloc (sizeof (time_t));
  973.         else
  974.           makefile_mtimes = (time_t *)
  975.             xrealloc ((char *) makefile_mtimes,
  976.                   (mm_idx + 1) * sizeof (time_t));
  977.         makefile_mtimes[mm_idx++] = file_mtime_no_search (d->file);
  978.         last = d;
  979.         d = d->next;
  980.           }
  981.       }
  982.       }
  983.  
  984.       /* Set up `MAKEFLAGS' specially while remaking makefiles.  */
  985.       define_makeflags (1, 1);
  986.  
  987.       switch (update_goal_chain (read_makefiles, 1))
  988.     {
  989.     case 1:
  990.     default:
  991. #define BOGUS_UPDATE_STATUS 0
  992.       assert (BOGUS_UPDATE_STATUS);
  993.       break;
  994.  
  995.     case -1:
  996.       /* Did nothing.  */
  997.       break;
  998.  
  999.     case 2:
  1000.       /* Failed to update.    Figure out if we care.    */
  1001.       {
  1002.         /* Nonzero if any makefile was successfully remade.  */
  1003.         int any_remade = 0;
  1004.         /* Nonzero if any makefile we care about failed
  1005.            in updating or could not be found at all.  */
  1006.         int any_failed = 0;
  1007.         register unsigned int i;
  1008.  
  1009.         for (i = 0; read_makefiles != 0; ++i)
  1010.           {
  1011.         struct dep *d = read_makefiles;
  1012.         read_makefiles = d->next;
  1013.         if (d->file->updated)
  1014.           {
  1015.             /* This makefile was updated.  */
  1016.             if (d->file->update_status == 0)
  1017.               {
  1018.             /* It was successfully updated.  */
  1019.             any_remade |= (file_mtime_no_search (d->file)
  1020.                        != makefile_mtimes[i]);
  1021.               }
  1022.             else if (! (d->changed & RM_DONTCARE))
  1023.               {
  1024.             time_t mtime;
  1025.             /* The update failed and this makefile was not
  1026.                from the MAKEFILES variable, so we care.  */
  1027.             error ("Failed to remake makefile `%s'.",
  1028.                    d->file->name);
  1029.             mtime = file_mtime_no_search (d->file);
  1030.             any_remade |= (mtime != (time_t) -1
  1031.                        && mtime != makefile_mtimes[i]);
  1032.               }
  1033.           }
  1034.         else
  1035.           /* This makefile was not found at all.  */
  1036.           if (! (d->changed & RM_DONTCARE))
  1037.             {
  1038.               /* This is a makefile we care about.  See how much.  */
  1039.               if (d->changed & RM_INCLUDED)
  1040.             /* An included makefile.  We don't need
  1041.                to die, but we do want to complain.    */
  1042.             error ("Included makefile `%s' was not found.",
  1043.                    dep_name (d));
  1044.               else
  1045.             {
  1046.               /* A normal makefile.  We must die later.  */
  1047.               error ("Makefile `%s' was not found", dep_name (d));
  1048.               any_failed = 1;
  1049.             }
  1050.             }
  1051.  
  1052.         free ((char *) d);
  1053.           }
  1054.  
  1055.         if (any_remade)
  1056.           goto re_exec;
  1057.         else if (any_failed)
  1058.           die (2);
  1059.         else
  1060.           break;
  1061.       }
  1062.  
  1063.     case 0:
  1064.     re_exec:
  1065.       /* Updated successfully.  Re-exec ourselves.    */
  1066.  
  1067.       remove_intermediates (0);
  1068.  
  1069.       if (print_data_base_flag)
  1070.         print_data_base ();
  1071.  
  1072.       if (print_directory_flag)
  1073.         log_working_directory (0);
  1074.  
  1075.       if (makefiles != 0)
  1076.         {
  1077.           /* These names might have changed.  */
  1078.           register unsigned int i, j = 0;
  1079.           for (i = 1; i < argc; ++i)
  1080.         if (!strcmp (argv[i], "-f")) /* XXX */
  1081.           {
  1082.             char *p = &argv[i][2];
  1083.             if (*p == '\0')
  1084.               argv[++i] = makefiles->list[j];
  1085.             else
  1086.               argv[i] = concat ("-f", makefiles->list[j], "");
  1087.             ++j;
  1088.           }
  1089.         }
  1090.  
  1091.       if (directories != 0 && directories->idx > 0)
  1092.         {
  1093.           char bad;
  1094.           if (directory_before_chdir != 0)
  1095.         {
  1096.           if (chdir (directory_before_chdir) < 0)
  1097.             {
  1098.               perror_with_name ("chdir", "");
  1099.               bad = 1;
  1100.             }
  1101.           else
  1102.             bad = 0;
  1103.         }
  1104.           else
  1105.         bad = 1;
  1106.           if (bad)
  1107.         fatal ("Couldn't change back to original directory.");
  1108.         }
  1109.  
  1110. #ifndef _AMIGA
  1111.       for (p = environ; *p != 0; ++p)
  1112.         if (!strncmp (*p, "MAKELEVEL=", 10))
  1113.           {
  1114.         /* The SGI compiler apparently can't understand
  1115.            the concept of storing the result of a function
  1116.            in something other than a local variable.  */
  1117.         char *sgi_loses;
  1118.         sgi_loses = (char *) alloca (40);
  1119.         *p = sgi_loses;
  1120.         sprintf (*p, "MAKELEVEL=%u", makelevel);
  1121.         break;
  1122.           }
  1123. #else /* AMIGA */
  1124. #   include <dos/dos.h>
  1125. #   include <proto/dos.h>
  1126.       {
  1127.         char buffer[256];
  1128.         int len;
  1129.  
  1130.         sprintf (buffer, "%u", makelevel);
  1131.         SetVar ("MAKELEVEL", buffer, -1, GVF_LOCAL_ONLY);
  1132.       }
  1133. #endif
  1134.  
  1135.       if (debug_flag)
  1136.         {
  1137.           char **p;
  1138.           fputs ("Re-executing:", stdout);
  1139.           for (p = argv; *p != 0; ++p)
  1140.         printf (" %s", *p);
  1141.           puts ("");
  1142.         }
  1143.  
  1144.       fflush (stdout);
  1145.       fflush (stderr);
  1146.  
  1147. #ifndef _AMIGA
  1148.       exec_command (argv, environ);
  1149. #else
  1150.       exec_command (argv);
  1151.       exit (0);
  1152. #endif
  1153.       /* NOTREACHED */
  1154.     }
  1155.     }
  1156.  
  1157.   /* Set up `MAKEFLAGS' again for the normal targets.  */
  1158.   define_makeflags (1, 0);
  1159.  
  1160.   {
  1161.     int status;
  1162.  
  1163.     /* If there were no command-line goals, use the default.  */
  1164.     if (goals == 0)
  1165.       {
  1166.     if (default_goal_file != 0)
  1167.       {
  1168.         goals = (struct dep *) xmalloc (sizeof (struct dep));
  1169.         goals->next = 0;
  1170.         goals->name = 0;
  1171.         goals->file = default_goal_file;
  1172.       }
  1173.       }
  1174.     else
  1175.       lastgoal->next = 0;
  1176.  
  1177.     if (goals != 0)
  1178.       {
  1179.     /* Update the goals.  */
  1180.  
  1181.     if (debug_flag)
  1182.       puts ("Updating goal targets....");
  1183.  
  1184.     switch (update_goal_chain (goals, 0))
  1185.       {
  1186.       case -1:
  1187.         /* Nothing happened.  */
  1188.       case 0:
  1189.         /* Updated successfully.  */
  1190.         status = 0;
  1191.         break;
  1192.       case 2:
  1193.         /* Updating failed.  */
  1194.         status = 2;
  1195.         break;
  1196.       case 1:
  1197.         /* We are under -q and would run some commands.  */
  1198.         status = 1;
  1199.         break;
  1200.       default:
  1201.         abort ();
  1202.       }
  1203.       }
  1204.     else
  1205.       {
  1206.     if (read_makefiles == 0)
  1207.       fatal ("No targets specified and no makefile found");
  1208.     else
  1209.       fatal ("No targets");
  1210.       }
  1211.  
  1212.     /* Exit.  */
  1213.     die (status);
  1214.   }
  1215.  
  1216.   return 0;
  1217. }
  1218.  
  1219. /* Parsing of arguments, decoding of switches.    */
  1220.  
  1221. static char options[1 + sizeof (switches) / sizeof (switches[0]) * 3];
  1222. static struct option long_options[(sizeof (switches) / sizeof (switches[0])) +
  1223.                   (sizeof (long_option_aliases) /
  1224.                    sizeof (long_option_aliases[0]))];
  1225.  
  1226. /* Fill in the string and vector for getopt.  */
  1227. static void
  1228. init_switches ()
  1229. {
  1230.   register char *p;
  1231.   register int c;
  1232.   register unsigned int i;
  1233.  
  1234.   if (options[0] != '\0')
  1235.     /* Already done.  */
  1236.     return;
  1237.  
  1238.   p = options;
  1239.  
  1240.   /* Return switch and non-switch args in order, regardless of
  1241.      POSIXLY_CORRECT.  Non-switch args are returned as option 1.  */
  1242.   *p++ = '-';
  1243.  
  1244.   for (i = 0; switches[i].c != '\0'; ++i)
  1245.     {
  1246.       long_options[i].name = (switches[i].long_name == 0 ? "" :
  1247.                   switches[i].long_name);
  1248.       long_options[i].flag = 0;
  1249.       long_options[i].val = switches[i].c;
  1250.       if (isalnum (switches[i].c))
  1251.     *p++ = switches[i].c;
  1252.       switch (switches[i].type)
  1253.     {
  1254.     case flag:
  1255.     case flag_off:
  1256.     case ignore:
  1257.       long_options[i].has_arg = no_argument;
  1258.       break;
  1259.  
  1260.     case string:
  1261.     case positive_int:
  1262.     case floating:
  1263.       if (isalnum (switches[i].c))
  1264.         *p++ = ':';
  1265.       if (switches[i].noarg_value != 0)
  1266.         {
  1267.           if (isalnum (switches[i].c))
  1268.         *p++ = ':';
  1269.           long_options[i].has_arg = optional_argument;
  1270.         }
  1271.       else
  1272.         long_options[i].has_arg = required_argument;
  1273.       break;
  1274.     }
  1275.     }
  1276.   *p = '\0';
  1277.   for (c = 0; c < (sizeof (long_option_aliases) /
  1278.            sizeof (long_option_aliases[0]));
  1279.        ++c)
  1280.     long_options[i++] = long_option_aliases[c];
  1281.   long_options[i].name = 0;
  1282. }
  1283.  
  1284. static void
  1285. handle_non_switch_argument (arg, env)
  1286.      char *arg;
  1287.      int env;
  1288. {
  1289.   /* Non-option argument.  It might be a variable definition.  */
  1290.   struct variable *v;
  1291.   if (arg[0] == '-' && arg[1] == '\0')
  1292.     /* Ignore plain `-' for compatibility.  */
  1293.     return;
  1294.   v = try_variable_definition ((char *) 0, 0, arg, o_command);
  1295.   if (v != 0)
  1296.     {
  1297.       /* It is indeed a variable definition.  Record a pointer to
  1298.      the variable for later use in define_makeflags.  */
  1299.       struct command_variable *cv
  1300.     = (struct command_variable *) xmalloc (sizeof (*cv));
  1301.       cv->variable = v;
  1302.       cv->next = command_variables;
  1303.       command_variables = cv;
  1304.     }
  1305.   else if (! env)
  1306.     {
  1307.       /* Not an option or variable definition; it must be a goal
  1308.      target!  Enter it as a file and add it to the dep chain of
  1309.      goals.  */
  1310.       struct file *f = enter_command_line_file (arg);
  1311.       f->cmd_target = 1;
  1312.  
  1313.       if (goals == 0)
  1314.     {
  1315.       goals = (struct dep *) xmalloc (sizeof (struct dep));
  1316.       lastgoal = goals;
  1317.     }
  1318.       else
  1319.     {
  1320.       lastgoal->next
  1321.         = (struct dep *) xmalloc (sizeof (struct dep));
  1322.       lastgoal = lastgoal->next;
  1323.     }
  1324.       lastgoal->name = 0;
  1325.       lastgoal->file = f;
  1326.     }
  1327. }
  1328.  
  1329. /* Decode switches from ARGC and ARGV.
  1330.    They came from the environment if ENV is nonzero.  */
  1331.  
  1332. static void
  1333. decode_switches (argc, argv, env)
  1334.      int argc;
  1335.      char **argv;
  1336.      int env;
  1337. {
  1338.   int bad = 0;
  1339.   register const struct command_switch *cs;
  1340.   register struct stringlist *sl;
  1341.   register int c;
  1342.  
  1343.   /* getopt does most of the parsing for us.
  1344.      First, get its vectors set up.  */
  1345.  
  1346.   init_switches ();
  1347.  
  1348.   /* Let getopt produce error messages for the command line,
  1349.      but not for options from the environment.    */
  1350.   opterr = !env;
  1351.   /* Reset getopt's state.  */
  1352.   optind = 0;
  1353.  
  1354.   while (optind < argc)
  1355.     {
  1356.       /* Parse the next argument.  */
  1357.       c = getopt_long (argc, argv, options, long_options, (int *) 0);
  1358.       if (c == EOF)
  1359.     /* End of arguments, or "--" marker seen.  */
  1360.     break;
  1361.       else if (c == 1)
  1362.     /* An argument not starting with a dash.  */
  1363.     handle_non_switch_argument (optarg, env);
  1364.       else if (c == '?')
  1365.     /* Bad option.    We will print a usage message and die later.
  1366.        But continue to parse the other options so the user can
  1367.        see all he did wrong.  */
  1368.     bad = 1;
  1369.       else
  1370.     for (cs = switches; cs->c != '\0'; ++cs)
  1371.       if (cs->c == c)
  1372.         {
  1373.           /* Whether or not we will actually do anything with
  1374.          this switch.  We test this individually inside the
  1375.          switch below rather than just once outside it, so that
  1376.          options which are to be ignored still consume args.  */
  1377.           int doit = !env || cs->env;
  1378.  
  1379.           switch (cs->type)
  1380.         {
  1381.         default:
  1382.           abort ();
  1383.  
  1384.         case ignore:
  1385.           break;
  1386.  
  1387.         case flag:
  1388.         case flag_off:
  1389.           if (doit)
  1390.             *(int *) cs->value_ptr = cs->type == flag;
  1391.           break;
  1392.  
  1393.         case string:
  1394.           if (!doit)
  1395.             break;
  1396.  
  1397.           if (optarg == 0)
  1398.             optarg = cs->noarg_value;
  1399.  
  1400.           sl = *(struct stringlist **) cs->value_ptr;
  1401.           if (sl == 0)
  1402.             {
  1403.               sl = (struct stringlist *)
  1404.             xmalloc (sizeof (struct stringlist));
  1405.               sl->max = 5;
  1406.               sl->idx = 0;
  1407.               sl->list = (char **) xmalloc (5 * sizeof (char *));
  1408.               *(struct stringlist **) cs->value_ptr = sl;
  1409.             }
  1410.           else if (sl->idx == sl->max - 1)
  1411.             {
  1412.               sl->max += 5;
  1413.               sl->list = (char **)
  1414.             xrealloc ((char *) sl->list,
  1415.                   sl->max * sizeof (char *));
  1416.             }
  1417.           sl->list[sl->idx++] = optarg;
  1418.           sl->list[sl->idx] = 0;
  1419.           break;
  1420.  
  1421.         case positive_int:
  1422.           if (optarg == 0 && argc > optind
  1423.               && isdigit (argv[optind][0]))
  1424.             optarg = argv[optind++];
  1425.  
  1426.           if (!doit)
  1427.             break;
  1428.  
  1429.           if (optarg != 0)
  1430.             {
  1431.               int i = atoi (optarg);
  1432.               if (i < 1)
  1433.             {
  1434.               if (doit)
  1435.                 error ("the `-%c' option requires a \
  1436. positive integral argument",
  1437.                    cs->c);
  1438.               bad = 1;
  1439.             }
  1440.               else
  1441.             *(unsigned int *) cs->value_ptr = i;
  1442.             }
  1443.           else
  1444.             *(unsigned int *) cs->value_ptr
  1445.               = *(unsigned int *) cs->noarg_value;
  1446.           break;
  1447.  
  1448. #ifndef NO_FLOAT
  1449.         case floating:
  1450.           if (optarg == 0 && optind < argc
  1451.               && (isdigit (argv[optind][0]) || argv[optind][0] == '.'))
  1452.             optarg = argv[optind++];
  1453.  
  1454.           if (doit)
  1455.             *(double *) cs->value_ptr
  1456.               = (optarg != 0 ? atof (optarg)
  1457.              : *(double *) cs->noarg_value);
  1458.  
  1459.           break;
  1460. #endif
  1461.         }
  1462.  
  1463.           /* We've found the switch.  Stop looking.  */
  1464.           break;
  1465.         }
  1466.     }
  1467.  
  1468.   /* There are no more options according to getting getopt, but there may
  1469.      be some arguments left.  Since we have asked for non-option arguments
  1470.      to be returned in order, this only happens when there is a "--"
  1471.      argument to prevent later arguments from being options.  */
  1472.   while (optind < argc)
  1473.     handle_non_switch_argument (argv[optind++], env);
  1474.  
  1475.  
  1476.   if (!env && (bad || print_usage_flag))
  1477.     {
  1478.       /* Print a nice usage message.  */
  1479.       FILE *usageto;
  1480.  
  1481.       if (print_version_flag)
  1482.     print_version ();
  1483.  
  1484.       usageto = bad ? stderr : stdout;
  1485.  
  1486.       fprintf (usageto, "Usage: %s [options] [target] ...\n", program);
  1487.  
  1488.       fputs ("Options:\n", usageto);
  1489.       for (cs = switches; cs->c != '\0'; ++cs)
  1490.     {
  1491.       char buf[1024], shortarg[50], longarg[50], *p;
  1492.  
  1493.       if (cs->description[0] == '-')
  1494.         continue;
  1495.  
  1496.       switch (long_options[cs - switches].has_arg)
  1497.         {
  1498.         case no_argument:
  1499.           shortarg[0] = longarg[0] = '\0';
  1500.           break;
  1501.         case required_argument:
  1502.           sprintf (longarg, "=%s", cs->argdesc);
  1503.           sprintf (shortarg, " %s", cs->argdesc);
  1504.           break;
  1505.         case optional_argument:
  1506.           sprintf (longarg, "[=%s]", cs->argdesc);
  1507.           sprintf (shortarg, " [%s]", cs->argdesc);
  1508.           break;
  1509.         }
  1510.  
  1511.       p = buf;
  1512.  
  1513.       if (isalnum (cs->c))
  1514.         {
  1515.           sprintf (buf, "  -%c%s", cs->c, shortarg);
  1516.           p += strlen (p);
  1517.         }
  1518.       if (cs->long_name != 0)
  1519.         {
  1520.           unsigned int i;
  1521.           sprintf (p, "%s--%s%s",
  1522.                !isalnum (cs->c) ? "  " : ", ",
  1523.                cs->long_name, longarg);
  1524.           p += strlen (p);
  1525.           for (i = 0; i < (sizeof (long_option_aliases) /
  1526.                    sizeof (long_option_aliases[0]));
  1527.            ++i)
  1528.         if (long_option_aliases[i].val == cs->c)
  1529.           {
  1530.             sprintf (p, ", --%s%s",
  1531.                  long_option_aliases[i].name, longarg);
  1532.             p += strlen (p);
  1533.           }
  1534.         }
  1535.       {
  1536.         const struct command_switch *ncs = cs;
  1537.         while ((++ncs)->c != '\0')
  1538.           if (ncs->description[0] == '-' &&
  1539.           ncs->description[1] == cs->c)
  1540.         {
  1541.           /* This is another switch that does the same
  1542.              one as the one we are processing.    We want
  1543.              to list them all together on one line.  */
  1544.           sprintf (p, ", -%c%s", ncs->c, shortarg);
  1545.           p += strlen (p);
  1546.           if (ncs->long_name != 0)
  1547.             {
  1548.               sprintf (p, ", --%s%s", ncs->long_name, longarg);
  1549.               p += strlen (p);
  1550.             }
  1551.         }
  1552.       }
  1553.  
  1554.       if (p - buf > DESCRIPTION_COLUMN - 2)
  1555.         /* The list of option names is too long to fit on the same
  1556.            line with the description, leaving at least two spaces.
  1557.            Print it on its own line instead.  */
  1558.         {
  1559.           fprintf (usageto, "%s\n", buf);
  1560.           buf[0] = '\0';
  1561.         }
  1562.  
  1563.       fprintf (usageto, "%*s%s.\n",
  1564.            - DESCRIPTION_COLUMN,
  1565.            buf, cs->description);
  1566.     }
  1567.  
  1568.       die (bad ? 2 : 0);
  1569.     }
  1570. }
  1571.  
  1572. /* Decode switches from environment variable ENVAR (which is LEN chars long).
  1573.    We do this by chopping the value into a vector of words, prepending a
  1574.    dash to the first word if it lacks one, and passing the vector to
  1575.    decode_switches.  */
  1576.  
  1577. static void
  1578. decode_env_switches (envar, len)
  1579.      char *envar;
  1580.      unsigned int len;
  1581. {
  1582.   char *varref = (char *) alloca (2 + len + 2);
  1583.   char *value, *p;
  1584.   int argc;
  1585.   char **argv;
  1586.  
  1587.   /* Get the variable's value.  */
  1588.   varref[0] = '$';
  1589.   varref[1] = '(';
  1590.   bcopy (envar, &varref[2], len);
  1591.   varref[2 + len] = ')';
  1592.   varref[2 + len + 1] = '\0';
  1593.   value = variable_expand (varref);
  1594.  
  1595.   /* Skip whitespace, and check for an empty value.  */
  1596.   value = next_token (value);
  1597.   len = strlen (value);
  1598.   if (len == 0)
  1599.     return;
  1600.  
  1601.   /* Allocate a vector that is definitely big enough.  */
  1602.   argv = (char **) alloca ((1 + len + 1) * sizeof (char *));
  1603.  
  1604.   /* Allocate a buffer to copy the value into while we split it into words
  1605.      and unquote it.  We must use permanent storage for this because
  1606.      decode_switches may store pointers into the passed argument words.  */
  1607.   p = (char *) xmalloc (2 * len);
  1608.  
  1609.   /* getopt will look at the arguments starting at ARGV[1].
  1610.      Prepend a spacer word.  */
  1611.   argv[0] = 0;
  1612.   argc = 1;
  1613.   argv[argc] = p;
  1614.   while (*value != '\0')
  1615.     {
  1616.       if (*value == '\\')
  1617.     ++value;        /* Skip the backslash.    */
  1618.       else if (isblank (*value))
  1619.     {
  1620.       /* End of the word.  */
  1621.       *p++ = '\0';
  1622.       argv[++argc] = p;
  1623.       do
  1624.         ++value;
  1625.       while (isblank (*value));
  1626.       continue;
  1627.     }
  1628.       *p++ = *value++;
  1629.     }
  1630.   *p = '\0';
  1631.   argv[++argc] = 0;
  1632.  
  1633.   if (argc == 2 && argv[1][0] != '-')
  1634.     {
  1635.       /* There is just one word in the value, and it is not a switch.
  1636.      Either this is the single-word form and we should prepend a dash
  1637.      before calling decode_switches, or this is the multi-word form and
  1638.      there is no dash because it is a variable definition.    */
  1639.       struct variable *v;
  1640.       v = try_variable_definition ((char *) 0, 0, argv[1], o_command);
  1641.       if (v != 0)
  1642.     {
  1643.       /* It was indeed a variable definition, and now it has been
  1644.          processed.  There is nothing for decode_switches to do.
  1645.          Record a pointer to the variable for later use in
  1646.          define_makeflags.    */
  1647.       struct command_variable *cv
  1648.         = (struct command_variable *) xmalloc (sizeof (*cv));
  1649.       cv->variable = v;
  1650.       cv->next = command_variables;
  1651.       command_variables = cv;
  1652.       return;
  1653.     }
  1654.  
  1655.       /* It wasn't a variable definition, so it's some switches without a
  1656.      leading dash.    Add one and pass it along to decode_switches.  We
  1657.      need permanent storage for this in case decode_switches saves
  1658.      pointers into the value.  */
  1659.       argv[1] = concat ("-", argv[1], "");
  1660.     }
  1661.  
  1662.   /* Parse those words.  */
  1663.   decode_switches (argc, argv, 1);
  1664. }
  1665.  
  1666. /* Quote the string IN so that it will be interpreted as a single word with
  1667.    no magic by the shell; if DOUBLE_DOLLARS is nonzero, also double dollar
  1668.    signs to avoid variable expansion in make itself.  Write the result into
  1669.    OUT, returning the address of the next character to be written.
  1670.    Allocating space for OUT twice the length of IN (thrice if
  1671.    DOUBLE_DOLLARS is nonzero) is always sufficient.  */
  1672.  
  1673. static char *
  1674. quote_as_word (out, in, double_dollars)
  1675.      char *out, *in;
  1676.      int double_dollars;
  1677. {
  1678.   while (*in != '\0')
  1679.     {
  1680.       if (index ("^;'\"*?[]$<>(){}|&~`\\ \t\r\n\f\v", *in) != 0)
  1681.     *out++ = '\\';
  1682.       if (double_dollars && *in == '$')
  1683.     *out++ = '$';
  1684.       *out++ = *in++;
  1685.     }
  1686.  
  1687.   return out;
  1688. }
  1689.  
  1690. /* Define the MAKEFLAGS and MFLAGS variables to reflect the settings of the
  1691.    command switches.  Include options with args if ALL is nonzero.
  1692.    Don't include options with the `no_makefile' flag set if MAKEFILE.  */
  1693.  
  1694. static void
  1695. define_makeflags (all, makefile)
  1696.      int all, makefile;
  1697. {
  1698.   static const char ref[] = "$(MAKEOVERRIDES)";
  1699.   static const char posixref[] = "$(-*-command-variables-*-)";
  1700.   register const struct command_switch *cs;
  1701.   char *flagstring;
  1702.   register char *p;
  1703.   unsigned int words;
  1704.   struct variable *v;
  1705.  
  1706.   /* We will construct a linked list of `struct flag's describing
  1707.      all the flags which need to go in MAKEFLAGS.  Then, once we
  1708.      know how many there are and their lengths, we can put them all
  1709.      together in a string.  */
  1710.  
  1711.   struct flag
  1712.     {
  1713.       struct flag *next;
  1714.       const struct command_switch *cs;
  1715.       char *arg;
  1716.     };
  1717.   struct flag *flags = 0;
  1718.   unsigned int flagslen = 0;
  1719. #define ADD_FLAG(ARG, LEN) \
  1720.   do {                                          \
  1721.     struct flag *new = (struct flag *) alloca (sizeof (struct flag));         \
  1722.     new->cs = cs;                                  \
  1723.     new->arg = (ARG);                                                         \
  1724.     new->next = flags;                                  \
  1725.     flags = new;                                  \
  1726.     if (new->arg == 0)                                                        \
  1727.       ++flagslen;        /* Just a single flag letter.  */          \
  1728.     else                                      \
  1729.       flagslen += 1 + 1 + 1 + 1 + 3 * (LEN); /* " -x foo" */                  \
  1730.     if (!isalnum (cs->c))                                                     \
  1731.       /* This switch has no single-letter version, so we use the long.    */    \
  1732.       flagslen += 2 + strlen (cs->long_name);                                 \
  1733.   } while (0)
  1734.  
  1735.   for (cs = switches; cs->c != '\0'; ++cs)
  1736.     if (cs->toenv && (!makefile || !cs->no_makefile))
  1737.       switch (cs->type)
  1738.     {
  1739.     default:
  1740.       abort ();
  1741.  
  1742.     case ignore:
  1743.       break;
  1744.  
  1745.     case flag:
  1746.     case flag_off:
  1747.       if (!*(int *) cs->value_ptr == (cs->type == flag_off)
  1748.           && (cs->default_value == 0
  1749.           || *(int *) cs->value_ptr != *(int *) cs->default_value))
  1750.         ADD_FLAG (0, 0);
  1751.       break;
  1752.  
  1753.     case positive_int:
  1754.       if (all)
  1755.         {
  1756.           if ((cs->default_value != 0
  1757.            && (*(unsigned int *) cs->value_ptr
  1758.                == *(unsigned int *) cs->default_value)))
  1759.         break;
  1760.           else if (cs->noarg_value != 0
  1761.                && (*(unsigned int *) cs->value_ptr ==
  1762.                *(unsigned int *) cs->noarg_value))
  1763.         ADD_FLAG ("", 0); /* Optional value omitted; see below.  */
  1764.           else if (cs->c == 'j')
  1765.         /* Special case for `-j'.  */
  1766.         ADD_FLAG ("1", 1);
  1767.           else
  1768.         {
  1769.           char *buf = (char *) alloca (30);
  1770.           sprintf (buf, "%u", *(unsigned int *) cs->value_ptr);
  1771.           ADD_FLAG (buf, strlen (buf));
  1772.         }
  1773.         }
  1774.       break;
  1775.  
  1776. #ifndef NO_FLOAT
  1777.     case floating:
  1778.       if (all)
  1779.         {
  1780.           if (cs->default_value != 0
  1781.           && (*(double *) cs->value_ptr
  1782.               == *(double *) cs->default_value))
  1783.         break;
  1784.           else if (cs->noarg_value != 0
  1785.                && (*(double *) cs->value_ptr
  1786.                == *(double *) cs->noarg_value))
  1787.         ADD_FLAG ("", 0); /* Optional value omitted; see below.  */
  1788.           else
  1789.         {
  1790.           char *buf = (char *) alloca (100);
  1791.           sprintf (buf, "%g", *(double *) cs->value_ptr);
  1792.           ADD_FLAG (buf, strlen (buf));
  1793.         }
  1794.         }
  1795.       break;
  1796. #endif
  1797.  
  1798.     case string:
  1799.       if (all)
  1800.         {
  1801.           struct stringlist *sl = *(struct stringlist **) cs->value_ptr;
  1802.           if (sl != 0)
  1803.         {
  1804.           /* Add the elements in reverse order, because
  1805.              all the flags get reversed below; and the order
  1806.              matters for some switches (like -I).  */
  1807.           register unsigned int i = sl->idx;
  1808.           while (i-- > 0)
  1809.             ADD_FLAG (sl->list[i], strlen (sl->list[i]));
  1810.         }
  1811.         }
  1812.       break;
  1813.     }
  1814.  
  1815.   flagslen += 4 + sizeof posixref; /* Four more for the possible " -- ".  */
  1816.  
  1817. #undef    ADD_FLAG
  1818.  
  1819.   /* Construct the value in FLAGSTRING.
  1820.      We allocate enough space for a preceding dash and trailing null.  */
  1821.   flagstring = (char *) alloca (1 + flagslen + 1);
  1822.   p = flagstring;
  1823.   words = 1;
  1824.   *p++ = '-';
  1825.   while (flags != 0)
  1826.     {
  1827.       /* Add the flag letter or name to the string.  */
  1828.       if (!isalnum (flags->cs->c))
  1829.     {
  1830.       *p++ = '-';
  1831.       strcpy (p, flags->cs->long_name);
  1832.       p += strlen (p);
  1833.     }
  1834.       else
  1835.     *p++ = flags->cs->c;
  1836.       if (flags->arg != 0)
  1837.     {
  1838.       /* A flag that takes an optional argument which in this case is
  1839.          omitted is specified by ARG being "".  We must distinguish
  1840.          because a following flag appended without an intervening " -"
  1841.          is considered the arg for the first.  */
  1842.       if (flags->arg[0] != '\0')
  1843.         {
  1844.           /* Add its argument too.    */
  1845.           *p++ = !isalnum (flags->cs->c) ? '=' : ' ';
  1846.           p = quote_as_word (p, flags->arg, 1);
  1847.         }
  1848.       ++words;
  1849.       /* Write a following space and dash, for the next flag.  */
  1850.       *p++ = ' ';
  1851.       *p++ = '-';
  1852.     }
  1853.       else if (!isalnum (flags->cs->c))
  1854.     {
  1855.       ++words;
  1856.       /* Long options must each go in their own word,
  1857.          so we write the following space and dash.    */
  1858.       *p++ = ' ';
  1859.       *p++ = '-';
  1860.     }
  1861.       flags = flags->next;
  1862.     }
  1863.  
  1864.   if (all && command_variables != 0)
  1865.     {
  1866.       /* Now write a reference to $(MAKEOVERRIDES), which contains all the
  1867.      command-line variable definitions.  */
  1868.  
  1869.       if (p == &flagstring[1])
  1870.     /* No flags written, so elide the leading dash already written.  */
  1871.     p = flagstring;
  1872.       else
  1873.     {
  1874.       /* Separate the variables from the switches with a "--" arg.  */
  1875.       if (p[-1] != '-')
  1876.         {
  1877.           /* We did not already write a trailing " -".  */
  1878.           *p++ = ' ';
  1879.           *p++ = '-';
  1880.         }
  1881.       /* There is a trailing " -"; fill it out to " -- ".  */
  1882.       *p++ = '-';
  1883.       *p++ = ' ';
  1884.     }
  1885.  
  1886.       /* Copy in the string.  */
  1887.       if (posix_pedantic)
  1888.     {
  1889.       bcopy (posixref, p, sizeof posixref - 1);
  1890.       p += sizeof posixref - 1;
  1891.     }
  1892.       else
  1893.     {
  1894.       bcopy (ref, p, sizeof ref - 1);
  1895.       p += sizeof ref - 1;
  1896.     }
  1897.     }
  1898.   else if (p == &flagstring[1])
  1899.     {
  1900.       words = 0;
  1901.       --p;
  1902.     }
  1903.   else if (p[-1] == '-')
  1904.     /* Kill the final space and dash.  */
  1905.     p -= 2;
  1906.   /* Terminate the string.  */
  1907.   *p = '\0';
  1908.  
  1909.   v = define_variable ("MAKEFLAGS", 9,
  1910.                /* If there is just a single word of switches,
  1911.               omit the leading dash unless it is a single
  1912.               long option with two leading dashes.    */
  1913.                &flagstring[(words == 1 && command_variables == 0
  1914.                     && flagstring[1] != '-')
  1915.                    ? 1 : 0],
  1916.                /* This used to use o_env, but that lost when a
  1917.               makefile defined MAKEFLAGS.  Makefiles set
  1918.               MAKEFLAGS to add switches, but we still want
  1919.               to redefine its value with the full set of
  1920.               switches.  Of course, an override or command
  1921.               definition will still take precedence.  */
  1922.                o_file, 1);
  1923.   if (! all)
  1924.     /* The first time we are called, set MAKEFLAGS to always be exported.
  1925.        We should not do this again on the second call, because that is
  1926.        after reading makefiles which might have done `unexport MAKEFLAGS'. */
  1927.     v->export = v_export;
  1928.   /* Since MFLAGS is not parsed for flags, there is no reason to
  1929.      override any makefile redefinition.  */
  1930.   (void) define_variable ("MFLAGS", 6, flagstring, o_env, 1);
  1931. }
  1932.  
  1933. /* Print version information.  */
  1934.  
  1935. static void
  1936. print_version ()
  1937. {
  1938.   static int printed_version = 0;
  1939.  
  1940.   char *precede = print_data_base_flag ? "# " : "";
  1941.  
  1942.   if (printed_version)
  1943.     /* Do it only once.  */
  1944.     return;
  1945.  
  1946.   printf ("%sGNU Make version %s", precede, version_string);
  1947.   if (remote_description != 0 && *remote_description != '\0')
  1948.     printf ("-%s", remote_description);
  1949.  
  1950.   printf (", by Richard Stallman and Roland McGrath.\n\
  1951. %sCopyright (C) 1988, 89, 90, 91, 92, 93, 94, 95 Free Software Foundation, Inc.\n\
  1952. %sThis is free software; see the source for copying conditions.\n\
  1953. %sThere is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n\
  1954. %sPARTICULAR PURPOSE.\n\n", precede, precede, precede, precede);
  1955.  
  1956.   printed_version = 1;
  1957.  
  1958.   /* Flush stdout so the user doesn't have to wait to see the
  1959.      version information while things are thought about.  */
  1960.   fflush (stdout);
  1961. }
  1962.  
  1963. /* Print a bunch of information about this and that.  */
  1964.  
  1965. static void
  1966. print_data_base ()
  1967. {
  1968.   extern char *ctime ();
  1969.   time_t when;
  1970.  
  1971.   when = time ((time_t *) 0);
  1972.   printf ("\n# Make data base, printed on %s", ctime (&when));
  1973.  
  1974.   print_variable_data_base ();
  1975.   print_dir_data_base ();
  1976.   print_rule_data_base ();
  1977.   print_file_data_base ();
  1978.   print_vpath_data_base ();
  1979.  
  1980.   when = time ((time_t *) 0);
  1981.   printf ("\n# Finished Make data base on %s\n", ctime (&when));
  1982. }
  1983.  
  1984. /* Exit with STATUS, cleaning up as necessary.    */
  1985.  
  1986. void
  1987. die (status)
  1988.      int status;
  1989. {
  1990.   static char dying = 0;
  1991.  
  1992.   if (!dying)
  1993.     {
  1994.       int err;
  1995.  
  1996.       dying = 1;
  1997.  
  1998.       /* Try to move back to the original directory.  This is essential on
  1999.      MS-DOS (where there is really only one process), and on Unix it
  2000.      puts core files in the original directory instead of the -C
  2001.      directory.  */
  2002.       if (directory_before_chdir != 0)
  2003.     chdir (directory_before_chdir);
  2004.  
  2005.       if (print_version_flag)
  2006.     print_version ();
  2007.  
  2008.       /* Wait for children to die.  */
  2009.       for (err = status != 0; job_slots_used > 0; err = 0)
  2010.     reap_children (1, err);
  2011.  
  2012.       /* Remove the intermediate files.  */
  2013.       remove_intermediates (0);
  2014.  
  2015.       if (print_data_base_flag)
  2016.     print_data_base ();
  2017.  
  2018.       if (print_directory_flag)
  2019.     log_working_directory (0);
  2020.     }
  2021.  
  2022.   exit (status);
  2023. }
  2024.  
  2025. /* Write a message indicating that we've just entered or
  2026.    left (according to ENTERING) the current directory.  */
  2027.  
  2028. static void
  2029. log_working_directory (entering)
  2030.      int entering;
  2031. {
  2032.   static int entered = 0;
  2033.   char *message = entering ? "Entering" : "Leaving";
  2034.  
  2035.   if (entering)
  2036.     entered = 1;
  2037.   else if (!entered)
  2038.     /* Don't print the leaving message if we
  2039.        haven't printed the entering message.  */
  2040.     return;
  2041.  
  2042.   if (print_data_base_flag)
  2043.     fputs ("# ", stdout);
  2044.  
  2045.   if (makelevel == 0)
  2046.     printf ("%s: %s ", program, message);
  2047.   else
  2048.     printf ("%s[%u]: %s ", program, makelevel, message);
  2049.  
  2050.   if (starting_directory == 0)
  2051.     puts ("an unknown directory");
  2052.   else
  2053.     printf ("directory `%s'\n", starting_directory);
  2054. }
  2055.